﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.IO;
using System.Text;

namespace WebSite.admin.product
{
    public partial class goods_add03 : System.Web.UI.Page
    {
        // Fields


        // Methods
        private void AddPicture(string imgpath, string imgnote, string albumid)
        {
            SQLAccess.DoSql("insert into DB_Spe(spepic,spename,proid) values('" + imgpath + "','" + imgnote + "'," + albumid + ")");
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Request.QueryString["pid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Shoppro set proorder=", this.proorder.Value == "" ? "0" : re.replace(this.proorder.Value), ",proweight1='", re.replace(this.proweight1.Value), "',proweight2='", re.replace(this.proweight2.Value), "',proku1=", re.replace(this.proku1.Value), ",proku2=", re.replace(this.proku2.Value), ",proisnew1=",  this.proisnew1.Checked  ? "1" : "0", ",proisnew2=",  this.proisnew2.Checked  ? "1" : "0", ",proisnew3=", this.proisnew3.Checked  ? "1" : "0", 
                ",proisnew4=",  this.proisnew4.Checked  ? "1" : "0", ",proisnews2=", this.CheckBox2.Checked  ? "1" : "0", ",proisnews1=",  this.CheckBox1.Checked  ? "1" : "0", ",proisnews3=", this.CheckBox3.Checked  ? "1" : "0", ",proisnews4=", this.CheckBox4.Checked  ? "1" : "0", ",proisnews5=",  this.CheckBox5.Checked  ? "1" : "0", ",proisshow=", this.proisshow.Checked  ? "1" : "0", ",proisp=",  this.proisp.Checked  ? "1" : "0", 
                ",prokey='", re.replace(this.prokey.Value), "',promark='", re.replace(this.promark.Text), "' where proid=", base.Request.QueryString["pid"], ""
             }));
                HttpFileCollection files = HttpContext.Current.Request.Files;
                StringBuilder builder = new StringBuilder("");
                string[] strArray = base.Request.Form["text"].Split(new char[] { ',' });
                string albumid = base.Request.QueryString["pid"].ToString();
                for (int i = 0; i < files.Count; i++)
                {
                    if (files[i].FileName.Length > 0)
                    {
                        HttpPostedFile file = files[i];
                        if ((file.ContentLength / 0x400) > 0x400)
                        {
                            builder.Append(Path.GetFileName(file.FileName) + "---不能大于1024k<br>");
                            break;
                        }
                        string extension = Path.GetExtension(file.FileName);
                        if (((extension != ".jpg") && (extension != ".JPG")) && ((extension != ".gif") && (extension != ".GIF")))
                        {
                            builder.Append(Path.GetFileName(file.FileName) + "---图片格式不对，只能是jpg或gif<br>");
                            break;
                        }
                    }
                }
                if (builder.Length <= 0)
                {
                    string str4 = "pic003";
                    string path = base.Server.MapPath("../../uploadfile/PicBase") + @"\" + str4;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    Random random = new Random();
                    int num2 = 1;
                    this.upcontent(albumid);
                    for (int j = 0; j < files.Count; j++)
                    {
                        HttpPostedFile file2 = files[j];
                        string fileName = "";
                        string str7 = "";
                        string imgpath = "";
                        fileName = Path.GetFileName(file2.FileName);
                        string str9 = random.Next(100, 0x5f5e100).ToString() + num2.ToString();
                        string str10 = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + str9;
                        if (fileName.Length > 0)
                        {
                            str7 = Path.GetExtension(file2.FileName);
                            string filename = path + @"\" + str10 + str7;
                            file2.SaveAs(filename);
                            imgpath = @"PicBase\" + str4 + @"\" + str10 + str7;
                        }
                        if (strArray[j].ToString() != "")
                        {
                            this.AddPicture(imgpath, strArray[j], albumid);
                        }
                        num2++;
                    }
                }
                base.Response.Write("<script language='javascript'>alert('产品其它信息修改成功');window.location.href='goods_add03.aspx?pid=" + base.Request.QueryString["pid"] + "';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton1.Attributes["onclick"] = "return clicka();";
            if (!base.IsPostBack && (base.Request.QueryString["pid"] != null))
            {
                this.upbind();
            }
        }

        private void upbind()
        {
            DataSet set = SQLAccess.GreatDs("select * from DB_Spe where proid=" + base.Request.QueryString["pid"] + " order by speid asc");
            this.DataList1.DataSource = set;
            this.DataList1.DataBind();
            string sql = "select top 1 * from DB_Shoppro where proid=" + base.Request.QueryString["pid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.proweight1.Value = table.Rows[0]["proweight1"].ToString();
            this.proweight2.Value = table.Rows[0]["proweight2"].ToString();
            this.proku1.Value = table.Rows[0]["proku1"].ToString();
            this.proku2.Value = table.Rows[0]["proku2"].ToString();
            this.proisnew1.Checked = table.Rows[0]["proisnew1"].ToString() =="" ? false : (bool)table.Rows[0]["proisnew1"];
            this.proisnew2.Checked = table.Rows[0]["proisnew2"].ToString() == "" ? false : (bool)table.Rows[0]["proisnew2"];
            this.proisnew3.Checked = table.Rows[0]["proisnew3"].ToString() == "" ? false : (bool)table.Rows[0]["proisnew3"];
            this.proisnew4.Checked = table.Rows[0]["proisnew4"].ToString() == "" ? false : (bool)table.Rows[0]["proisnew4"];
            this.proisshow.Checked = (bool)table.Rows[0]["proisshow"];
            this.proisp.Checked = (bool)table.Rows[0]["proisp"];
            this.prokey.Value = table.Rows[0]["prokey"].ToString();
            this.promark.Text = table.Rows[0]["promark"].ToString();
            this.proorder.Value = table.Rows[0]["proorder"].ToString();
            this.CheckBox1.Checked = table.Rows[0]["proisnews1"].ToString() == "" ? false : (bool)table.Rows[0]["proisnews1"];
            this.CheckBox2.Checked = table.Rows[0]["proisnews2"].ToString() == "" ? false : (bool)table.Rows[0]["proisnews2"];
            this.CheckBox3.Checked = table.Rows[0]["proisnews3"].ToString() == "" ? false : (bool)table.Rows[0]["proisnews3"];
            this.CheckBox4.Checked = table.Rows[0]["proisnews4"].ToString() == "" ? false : (bool)table.Rows[0]["proisnews4"];
            this.CheckBox5.Checked = table.Rows[0]["proisnews5"].ToString() == "" ? false : (bool)table.Rows[0]["proisnews5"];
            if (((base.Request.QueryString["speid"] != null) && (base.Request.QueryString["action"] != null)) && (base.Request.QueryString["action"].ToString() == "del"))
            {
                SQLAccess.DoSql("delete from DB_Spe where speid=" + base.Request.QueryString["speid"].ToString() + "");
                base.Response.Write("<script language='javascript'>window.location.href='goods_add03.aspx?pid=" + base.Request.QueryString["pid"] + "';</script>");
                base.Response.End();
            }
        }

        private void upcontent(string albumid)
        {
            string sql = "select * from DB_Spe where proid=" + albumid + " order by speid asc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    SQLAccess.DoSql("update [DB_Spe] set [spename]='" + base.Request.Form["content_" + row["speid"].ToString()] + "' where speid=" + row["speid"].ToString() + "");
                }
            }
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}